package com.wish.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wish.entity.SmsRecords;
import com.wish.entity.SysUser;
import com.wish.entity.SysUserToken;
import com.wish.service.ISmsRecordsService;
import com.wish.service.ISysUserService;
import com.wish.service.ISysUserTokenService;
import com.wish.util.WebUtils;
import com.wish.vo.ResultEntity;
import org.apache.commons.lang.RandomStringUtils;
import org.apache.shiro.crypto.hash.Sha256Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/wish/sys-login")
public class LoginController {

    @Autowired
    private ISmsRecordsService smsRecordsService;

    @Autowired
    private ISysUserService sysUserService;

    /*@Autowired
    private ISysUserTokenService sysUserTokenService;*/

    @Resource
    PasswordEncoder passwordEncoder;

    @RequestMapping("/getCode")
    public ResultEntity getCode(SmsRecords smsRecords){
        System.out.println("=========="+smsRecords);
        QueryWrapper<SmsRecords> wrapper = new QueryWrapper<>();
        wrapper.eq("mobile",smsRecords.getMobile());
        List<SmsRecords> list = smsRecordsService.list(wrapper);
        if(list.size()>0){
            SmsRecords records = list.get(list.size()-1);
            Date nowDate = new Date();
            if((nowDate.getTime() - records.getCreateTime().getTime()) / 1000 < 120){
                return ResultEntity.error("请勿2分钟内重复发送短信",false);
            }
        }
        StringBuffer sb = new StringBuffer();
        for(int i = 0; i<6;i++){
            sb.append((int)(Math.random()*10));
        }
        smsRecords.setMobileCode(sb.toString());
        smsRecords.setCreateTime(new Date());
        smsRecords.setParam("{\"code\":\""+sb.toString()+"\"}");
        smsRecords.setStatus("101");
        smsRecordsService.save(smsRecords);
        return ResultEntity.ok(smsRecords);
    }
    @RequestMapping("reg")
    public ResultEntity reg(@RequestBody SysUser sysUser, HttpServletResponse response){
        QueryWrapper<SysUser> wrapper = new QueryWrapper<>();
        wrapper.eq("mobile",sysUser.getMobile());
        List<SysUser> list = sysUserService.list(wrapper);
        if(list.size()>0){
            return ResultEntity.error("用户已存在!",false);
        }
        QueryWrapper<SmsRecords> wrapper1 = new QueryWrapper<>();
        wrapper1.eq("mobile",sysUser.getMobile());
        List<SmsRecords> list1 = smsRecordsService.list(wrapper1);
        if(list1.size()>0){
            SmsRecords records = list1.get(list1.size()-1);
            Date nowDate = new Date();
            if((nowDate.getTime() - records.getCreateTime().getTime()) / 1000 > 120){
                return ResultEntity.error("验证码已过期!!",false);
            }
            QueryWrapper<SmsRecords> wrapper2 = new QueryWrapper<>();
            wrapper2.eq("mobile",sysUser.getMobile());
            wrapper2.eq("mobile_code",sysUser.getCode());
            List<SmsRecords> list2 = smsRecordsService.list(wrapper2);
            if(list2.size()<1){
                return ResultEntity.error("验证码错误!!",false);
            }
        }
        sysUser.setCreateTime(new Date());
        /*String salt = RandomStringUtils.randomAlphanumeric(20);
        sysUser.setPassword(new Sha256Hash(sysUser.getPassword(), salt).toHex());
        sysUser.setSalt(salt);*/


        String encode = passwordEncoder.encode(sysUser.getPassword());
        sysUser.setPassword(encode);

        sysUser.setContactMember(sysUser.getUsername());
        sysUser.setUsername(sysUser.getMobile());
        sysUser.setDelFlag(0);
        sysUser.setStatus(1);
        sysUser.setApprovalFlag(4);
        sysUserService.save(sysUser);
        /*SysUserToken sysUserToken = sysUserTokenService.createToken(sysUser.getUserId());
        WebUtils.setCookie(response, "token", (String)sysUserToken.getToken(), (int)sysUserToken.getExpireTime().getTime());*/
        return ResultEntity.ok(sysUser.getUserId());
    }
}
